package dyy.zc.note.ui.activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.note.R;
import dyy.zc.note.application.MyApplication;
import dyy.zc.note.base.BaseActivity;
import dyy.zc.note.constant.Constants;
import dyy.zc.note.permission.PermissionsChecker;
import dyy.zc.note.utils.BmobUtils;
import dyy.zc.note.utils.ImageLoaderUtils;
import dyy.zc.note.utils.ImageUploadUtil;
import dyy.zc.note.utils.ImageUtil;
import dyy.zc.note.utils.ToastUtils;
import dyy.zc.note.views.ActionSheet;
import dyy.zc.note.views.CircleImageView;


public class EditInfoActivity extends BaseActivity implements View.OnClickListener,ActionSheet.OnSheetItemClickListener{
    private TextView title,ok;
    private EditText name,sex,age,xingzuo,birth,company,school,hometown,email;
    private CircleImageView head;
    private BmobUtils bmobUtils;

    private String[] items = {"拍照", "我的相册"};
    // 头像文件、上传头像的名称、本地图片uri
    private File imageFile;
    private Uri imageUri; // 图片路径
    public ImageView civ_head;
    private String saveFileName = "";
    private PermissionsChecker mPermissionsChecker; // 权限检测器
    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
    };
    public static int type = 0;
    //拍照、从相册选择、照片保存成功
    private final int TAKE_PHOTO = 1, CHOOSE_PHOTO = 2, SAVE_IMAGE_SUCCESS = 3;
    private final int REQUEST_CODE = 0;

    private boolean headFinish = false;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constants.ResultCode.SUCCESS:
                    dialog.dismiss();
                    headFinish = true;
                    ImageLoaderUtils.initImage(EditInfoActivity.this,loginuser.getHead().getFileUrl(),head, R.drawable.chat_xe_icon2_03);
                    break;
                case Constants.ResultCode.FAILED:
                    dialog.dismiss();
                    ToastUtils.shortToast(EditInfoActivity.this,"图片上传失败，请重试");
                    break;
                case Constants.ResultCode.RESULT_SUCCESS:
                    dialog.dismiss();
                    startActivity(MainActivity.class);
                    finish();
                    break;
            }
        }
    };
    @Override
    protected void findViewByIDS() {
        title = findViewsById(R.id.top_tv_title);
        ok = findViewsById(R.id.top_tv_right);
        head = findViewsById(R.id.edit_myinfo_civ_head);
        name = findViewsById(R.id.edit_myinfo_et_name);
        sex = findViewsById(R.id.edit_myinfo_et_sex);
        age = findViewsById(R.id.edit_myinfo_et_age);
//        xingzuo = findViewsById(R.id.edit_myinfo_et_xingzuo);
        birth = findViewsById(R.id.edit_myinfo_et_birth);
        email = findViewsById(R.id.edit_myinfo_et_email);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Log.e("asdf",loginuser.isNewInfo()+"");
//        if (loginuser.isNewInfo()){
//            startActivity(MainActivity.class);
//            finish();
//        }
        setContentView(R.layout.activity_edit_info);
        initData();
        bind();
    }

    private void bind() {
        head.setOnClickListener(this);
        findViewsById(R.id.top_rl_right).setOnClickListener(this);
    }

    private void initData() {
        title.setText("个人信息");
        ok.setText("确定");
        ok.setVisibility(View.VISIBLE);
        bmobUtils = new BmobUtils(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.edit_myinfo_civ_head:
                showActionSheetDialog(items);
                break;
            case R.id.top_rl_right:
                if (headFinish){
                    if (!name.getText().toString().equals("")){
                        dialog = ProgressDialog.show(EditInfoActivity.this,null,"正在保存个人信息");
                        loginuser.setName(name.getText().toString());
                        loginuser.setAge(age.getText().toString());
                        loginuser.setSex(sex.getText().toString());
                        bmobUtils.EditInfpUpdateInfo(loginuser);
                    }else {
                        ToastUtils.shortToast(EditInfoActivity.this,"请输入名字");
                    }

                }
                break;
        }
    }

    //    //头像选择对话框
    private void showActionSheetDialog(String[] items) {
        ActionSheet actionSheet = new ActionSheet(this)
                .builder()
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (int i = 0; i < items.length; i++) {
            actionSheet.addSheetItem(items[i], ActionSheet.SheetItemColor.Blue, EditInfoActivity.this);
        }
        actionSheet.show();

    }

    /**
     * 退出登录提示框
     */
//    private void logoutDialog() {
//        MyAlertDialog logDialog = new MyAlertDialog(getActivity());
//        logDialog.builder()
//                .setTitle("退出提示")
//                .setMsg("是否要退出登录？")
//                .setNegativeButton("取消", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//
//                    }
//                })
//                .setPositiveButton("确定", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        MyApplication.getInstance().saveUserInfo(null);
//                        Intent intent = new Intent(getActivity(), LoginActivity.class);
//                        startActivity(intent);
//                        AppManager.getInstance().finishActivity();
//                    }
//                })
//                .show();
//    }

    @Override
    public void onClick(int which) {
        switch (which) {
            case TAKE_PHOTO:
                //拍照
                imageFile = ImageUploadUtil.createImageFile(ImageUploadUtil.CAMERA_SAVEDIR2, ImageUploadUtil.createImageName());
                startActivityForResult(ImageUploadUtil.intentImageCapture(imageFile), ImageUploadUtil.TAKE_PHOTO);
                break;
            case CHOOSE_PHOTO:
                //从相册选择
                startActivityForResult(ImageUtil.intentChooseImg(),
                        CHOOSE_PHOTO);
                break;
        }
    }
//
//    /**
//     * Activity回调事件处理
//     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == 0) {
                    return;
                }
                //拍照
                ImageUtil.paizhaocreateImagefile(this, imageFile);
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "图片太大无法上传");
                    return;
                }
                doSendThread();//上传图片
                break;
            case CHOOSE_PHOTO:
                //相册
                if (resultCode == 0) return;
                imageUri = data.getData();
                imageFile = MyApplication.getInstance().createimagefile(imageUri, getVmWidth(), getVmHeight());
                if (imageFile == null) {
                    ToastUtils.shortToast(this, "imageFile为空2");
                    return;
                }
                doSendThread();//上传图片
                break;
        }
    }
//
//    /**
//     * 上传头像
//     */
    private void doSendThread() {
//        Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getPath());
//        civ_head.setImageBitmap(bitmap);
        dialog = ProgressDialog.show(this,null,"正在上传头像");
        BmobFile bmobFile = new BmobFile(imageFile);
        bmobUtils.EditInfoUpLoadFile(bmobFile,loginuser);
//        bmobUtils.upLoadFile(bmobFile,imageFile);
        //initInfo();
    }


}
